package com.dhcy.mqtt.controller;

import com.dhcy.mqtt.config.mqtt.MqttConfig;
import com.dhcy.mqtt.model.DeviceType;
import com.dhcy.mqtt.service.DeviceTypeService;
import com.dhcy.mqtt.service.MqttGateway;
import com.dhcy.mqtt.util.AppResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("deviceType")
public class DeviceTypeController {

    @Autowired
    DeviceTypeService deviceTypeService;

    @Autowired
    private MqttGateway mqttGateway;

    @Autowired
    private MqttConfig mqttConfig;

    @PostMapping("add")
    @ResponseBody
    public AppResultData Add(String reqdata) {
        DeviceType deviceType = new DeviceType();
        String[] s = reqdata.split("\\$");
        switch (s[0].substring(1)) {
            case "01":
                deviceType.setDeviceStyle("氚");
                break;
            case "02":
                deviceType.setDeviceStyle("气溶胶");
                break;
            case "03":
                deviceType.setDeviceStyle("中子");
                break;
            case "04":
                deviceType.setDeviceStyle("伽马");
                break;
        }
        if ("气溶胶".equals(deviceType.getDeviceStyle())) {
            deviceType.setDeviceNum(s[1]);
            deviceType.setYouValue(s[2]);
            deviceType.setBuValue(s[3]);
            switch (s[4].substring(0, s[4].length() - 1)) {
                case "01":
                    deviceType.setStatus("测量中");
                    break;
                case "02":
                    deviceType.setStatus("空闲");
                    break;
                case "03":
                    deviceType.setStatus("纸少于10%");
                    break;
                case "04":
                    deviceType.setStatus("缺纸");
                    break;
                case "05":
                    deviceType.setStatus("超阈值");
                    break;
            }
        } else {
            deviceType.setDeviceNum(s[1]);
            deviceType.setValue(s[2]);
            switch (s[3].substring(0, s[3].length() - 1)) {
                case "01":
                    deviceType.setStatus("正常");
                    break;
                case "02":
                    deviceType.setStatus("超阈值");
                    break;
            }
        }

        return deviceTypeService.deviceTypeAdd(deviceType);
    }

}
